package com.patent.service.impl;

import com.patent.common.PatentConstant;
import com.patent.common.ResultCode;
import com.patent.common.ResultData;
import com.patent.dao.InvoiceMapper;
import com.patent.domain.po.Invoice;
import com.patent.domain.po.InvoiceAmount;
import com.patent.service.InvoiceAmountService;
import com.patent.service.InvoiceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("invoiceService")
@Transactional
public class InvoiceServiceImpl implements InvoiceService {

	private static final Logger log = LoggerFactory.getLogger(InvoiceServiceImpl.class);

	@Resource(name="invoiceMapper")
	private InvoiceMapper invoiceMapper;

	@Resource(name="invoiceAmountService")
	private InvoiceAmountService invoiceAmountService;

	@Override
	public synchronized void addInvoice(Invoice invoice) {
		try {
			invoiceMapper.insertSelective(invoice);
			//修改可开发票金额
			invoiceAmountService.dealInvoiceAmount("reduce",invoice.getUserMobile(), invoice.getInvoiceAmount());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("添加发票失败：" + e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Invoice> getInvoiceList(String userMobile, Integer page, Integer rows) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("userMobile", userMobile);
		queryMap.put("page",(page-1)*rows);
		queryMap.put("rows",rows);
		List<Invoice> lists = invoiceMapper.getInvoiceList(queryMap);
		return lists;
	}

	@Override
	public ResultData cancelInvoice(String userMobile, String serialNumber) throws Exception {
		ResultData resultData = new ResultData();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userMobile", userMobile);
		map.put("serialNumber", serialNumber);
		Invoice invoice = invoiceMapper.selectInvoiceByMap(map);
		if(invoice == null){
			resultData.setCode(ResultCode.dataNotMatchCode.getCode());
			resultData.setMsg(ResultCode.dataNotMatchCode.getMessage());
			return resultData;
		}else{
			if(!PatentConstant.HAS_SUBMIT.equals(invoice.getInvoiceStatu())){
				resultData.setCode(ResultCode.invoiceStatuError.getCode());
				resultData.setMsg(ResultCode.invoiceStatuError.getMessage());
				return resultData;
			}else if(PatentConstant.HAS_SUBMIT.equals(invoice.getInvoiceStatu())){
				invoice.setInvoiceStatu(PatentConstant.CANCEL);
				invoiceMapper.updateByPrimaryKeySelective(invoice);
				invoiceAmountService.dealInvoiceAmount("add",invoice.getUserMobile(), invoice.getInvoiceAmount());
				resultData.setCode(ResultCode.success.getCode());
				resultData.setMsg(ResultCode.success.getMessage());
			}
		}
		return resultData;
	}

	/**
	 * 获取用户可开发票总额及最近一次开票信息
	 */
	@Override
	public ResultData getInvoiceInfo(String userMobile) {
		ResultData resultData = new ResultData();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			List<Invoice> list = getInvoiceList(userMobile, 1, 1);
			if(list != null && list.size() > 0){
				map.put("invoice", list.get(0));
			}else{
				map.put("invoice", null);
			}
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("userMobile", userMobile);
			InvoiceAmount invoiceAmount = invoiceAmountService.selectByMap(queryMap);
			if(invoiceAmount != null){
				map.put("amount",invoiceAmount.getAmount());
			}else{
				map.put("amount", 0);
			}
			resultData.setCode(ResultCode.success.getCode());
			resultData.setMsg(ResultCode.success.getMessage());
			resultData.setResult(map);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("查询发票信息失败：" + e.getMessage());
			throw new RuntimeException(e);
		}
		return resultData;
	}
}
